#include "ClassEditForm.h"
#include "ui_ClassEditForm.h"


#include "DataViewDelegate.h"



#include <CharacterItem.h>
#include <CampaignItem.h>
#include <D20CampaignModel.h>
#include <ClassEditModel.h>





#include <QFileDialog>



ClassEditFormBase::ClassEditFormBase(ItemRoot* rootItem, QWidget *parent) :
    QWidget(parent),
    BaseEditForm(rootItem, new D20CharacterModel(this)),
    ui(new Ui::ClassEditForm)
{
    m_campaignModel = new D20CampaignModel(this);

    initialize(this);
    ui->setupUi(this);

    ui->treeViewClassData->setModel(model());
    ui->treeViewClassData->setItemDelegate(new DataViewDelegate(this));
    ui->treeViewClassData->header()->setResizeMode(QHeaderView::ResizeToContents);

    openCampaign(root()->data(CharacterItem::CampaignData).toString());


    ClassEditModel* editModel = new ClassEditModel(this);
    editModel->setCampaignModel(m_campaignModel);
    editModel->setClassModel(dynamic_cast<D20CharacterModel*>(model()));
    ui->treeViewClassEdit->setModel(editModel);


    connect(FileHandler::instance(), SIGNAL(sigFileOpened()), this, SLOT(onFileOpened()));

}

ClassEditFormBase::~ClassEditFormBase()
{
    delete ui;
}


void ClassEditFormBase::updateData()
{

}


void ClassEditFormBase::openCampaign(const QString &file_name)
{
    if (!root() || file_name.isEmpty()) return;

    CampaignItem* campaignRoot = dynamic_cast<CampaignItem*>(FileHandler::open(file_name));
    if (!campaignRoot)
    {
        return;
    }

    m_campaignModel->setRootItem(campaignRoot);

    ui->comboBoxCampaign->blockSignals(true);

    int index = ui->comboBoxCampaign->findData(campaignRoot->filePath());

    if (index < 0)
    {
        index = ui->comboBoxCampaign->count();
        ui->comboBoxCampaign->insertItem(
                index,
                QString("%1 (%2)").arg(campaignRoot->name()).arg(campaignRoot->filePath()),
                campaignRoot->filePath());
    }

    ui->comboBoxCampaign->setCurrentIndex(index);
    ui->comboBoxCampaign->blockSignals(false);

    root()->setData(CharacterItem::CampaignData, file_name);
}


void ClassEditFormBase::onFileOpened()
{
    ui->comboBoxCampaign->blockSignals(true);
    foreach (ItemRoot* rootItem,FileHandler::openItems())
    {
        if (!dynamic_cast<CampaignItem*>(rootItem)) continue;

        if (ui->comboBoxCampaign->findData(rootItem->filePath()) >= 0) continue;

        ui->comboBoxCampaign->insertItem(
                ui->comboBoxCampaign->count(),
                QString("%1 (%2)").arg(rootItem->name()).arg(rootItem->filePath()),
                rootItem->filePath());
    }
    ui->comboBoxCampaign->blockSignals(false);
}

// custom autoconnect slots


// form autoconnect slots
void ClassEditFormBase::on_pushButtonOpenCampaign_clicked()
{
    openCampaign(QFileDialog::getOpenFileName(this, tr("Open Campaign..."), QDir::currentPath(), tr("Xml File (*.xml)")));
}



void ClassEditFormBase::on_comboBoxCampaign_currentIndexChanged(int index)
{
    openCampaign(ui->comboBoxCampaign->itemData(index).toString());
}


